package com.jyw.business.de.controller;

import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import com.alibaba.excel.EasyExcel;
import com.jyw.business.de.constant.DemandSourceConstant;
import com.jyw.business.de.constant.DemandTypeConstant;
import com.jyw.business.de.domain.dto.web.*;
import com.jyw.business.de.domain.vo.web.*;
import com.jyw.business.de.service.IDeDemandAllocationService;
import com.jyw.business.de.service.IDeDemandProcessService;
import com.jyw.business.de.service.IDeTaskInfoService;
import com.jyw.common.core.domain.CommonResult;
import com.jyw.common.core.domain.model.SimpleDto;
import com.jyw.common.core.page.BasePage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.core.io.ClassPathResource;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.jyw.common.annotation.Log;
import com.jyw.common.core.controller.BaseController;
import com.jyw.common.enums.BusinessType;
import com.jyw.business.de.service.IDeDemandInfoService;
import com.jyw.common.core.page.TableDataInfo;

/**
 * 需求-需求信息Controller
 * 
 * @author ruoyi
 * @date 2024-02-19
 */
@RestController
@RequestMapping("/de/demand/info")
@Api(tags = "需求池")
public class DeDemandInfoController extends BaseController
{
    @Autowired
    private IDeDemandInfoService deDemandInfoService;

    @Resource
    private IDeDemandAllocationService deDemandAllocationService;

    @Resource
    private IDeTaskInfoService taskInfoService;

    @Resource
    private IDeDemandProcessService processService;

    /**
     * 查询需求-需求信息列表
     */
    @PostMapping("/list")
    @ApiOperation("查询需求")
    public TableDataInfo<DemandInfoPageVO> list(@RequestBody DemandInfoPageDto dto, BasePage page)
    {
        List<DemandInfoPageVO> list = deDemandInfoService.demandPage(dto,page);
        return getDataTable(list);
    }

    /**
     * 查询需求-需求信息列表
     */
    @PostMapping("/export")
    @ApiOperation("导出需求")
    public void export(@RequestBody DemandInfoPageDto dto, HttpServletResponse response) throws IOException {
        List<DemandInfoPageVO> list = deDemandInfoService.demandPage(dto, null);

        // 这里注意 有同学反应使用swagger 会导致各种问题，请直接用浏览器或者用postman
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");
        // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
        String fileName = URLEncoder.encode("需求列表", "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
        // 这里 会填充到第一个sheet， 然后文件流会自动关闭
        EasyExcel.write(response.getOutputStream())
                .withTemplate(new ClassPathResource("excelTemplate/demand_page_list.xlsx").getInputStream())
                .sheet().doFill(list);

    }

    /**
     * 新增需求-需求信息
     */
    @PreAuthorize("@ss.hasPermi('de:demand:info:add')")
    @Log(title = "需求-需求信息", businessType = BusinessType.INSERT)
    @PostMapping
    @ApiOperation("新增需求")
    public CommonResult add(@RequestBody @Valid DemandInfoAddDto dto)
    {
        //设置需求来源为新增
        dto.setDemandSource(DemandSourceConstant.ADD);
        //设置需求类型为新增
        dto.setDemandType(DemandTypeConstant.ADD);
        return CommonResult.success(deDemandInfoService.insertDeDemandInfo(dto));
    }

    /**
     * 修改需求-需求信息
     */
    @PreAuthorize("@ss.hasPermi('de:demand:info:edit')")
    @Log(title = "需求-需求信息", businessType = BusinessType.UPDATE)
    @PutMapping
    @ApiOperation("编辑需求")
    public CommonResult edit(@RequestBody @Valid DemandInfoEditDto dto)
    {
        return CommonResult.success(deDemandInfoService.updateDeDemandInfo(dto));
    }


    @PreAuthorize("@ss.hasPermi('de:demand:info:allocation')")
    @PutMapping("/allocation")
    @ApiOperation("需求分配")
    public CommonResult allocation(@RequestBody @Valid DemandAllocationDto dto)
    {
        deDemandAllocationService.allocation(dto);
        return CommonResult.success();
    }


    /**
     * 接收需求
     */
    @PreAuthorize("@ss.hasPermi('de:demand:info:accept')")
    @PutMapping("accept")
    @ApiOperation("接收需求")
    public CommonResult accept(@RequestBody SimpleDto<Long> dto)
    {
        deDemandInfoService.acceptDemand(dto.getData());
        return CommonResult.success();
    }

    /**
     * 退回需求
     */
    @PreAuthorize("@ss.hasPermi('de:demand:info:back')")
    @PutMapping("back")
    @ApiOperation("退回需求")
    public CommonResult back(@RequestBody @Valid DemandOperDto dto)
    {
        deDemandInfoService.back(dto);
        return CommonResult.success();
    }

    /**
     * 调研需求
     */
    @PreAuthorize("@ss.hasPermi('de:demand:info:research')")
    @PutMapping("research")
    @ApiOperation("调研需求")
    public CommonResult research(@RequestBody @Valid DemandResearchAddDto dto)
    {
        deDemandInfoService.research(dto);
        return CommonResult.success();
    }

    /**
     * 转交需求
     */
    @PreAuthorize("@ss.hasPermi('de:demand:info:transfer')")
    @PutMapping("transfer")
    @ApiOperation("转交需求")
    public CommonResult transfer(@RequestBody @Valid TransferDto dto)
    {
        deDemandInfoService.transfer(dto);
        return CommonResult.success();
    }

    /**
     * 暂停需求
     */
    @PreAuthorize("@ss.hasPermi('de:demand:info:pause')")
    @PutMapping("pause")
    @ApiOperation("暂停需求")
    public CommonResult pause(@RequestBody @Valid DemandOperDto dto)
    {
        deDemandInfoService.pause(dto);
        return CommonResult.success();
    }

    /**
     * 暂停需求
     */
    @PreAuthorize("@ss.hasPermi('de:demand:info:open')")
    @PutMapping("open")
    @ApiOperation("开启需求")
    public CommonResult open(@RequestBody SimpleDto<Long> dto)
    {
        deDemandInfoService.open(dto);
        return CommonResult.success();
    }

    /**
     * 暂停需求
     */
    @PreAuthorize("@ss.hasPermi('de:demand:info:termination')")
    @PutMapping("termination")
    @ApiOperation("终止需求")
    public CommonResult termination(@RequestBody @Valid DemandOperDto dto)
    {
        deDemandInfoService.termination(dto);
        return CommonResult.success();
    }




    /**
     * 创建任务
     */
    @PreAuthorize("@ss.hasPermi('de:demand:info:addTask')")
    @PostMapping("addTask")
    @ApiOperation("创建任务")
    public CommonResult addTask(@RequestBody @Valid TaskAddDto dto)
    {
        taskInfoService.demandAddTask(dto);;
        return CommonResult.success();
    }

    /**
     * 创建任务
     */
    @PreAuthorize("@ss.hasPermi('de:demand:info:editTask')")
    @PostMapping("editTask")
    @ApiOperation("编辑任务")
    public CommonResult editTask(@RequestBody @Valid TaskEditDto dto)
    {
        taskInfoService.demandEditTask(dto);;
        return CommonResult.success();
    }

    /**
     * 需求分析
     */
    @PreAuthorize("@ss.hasPermi('de:demand:info:analyze')")
    @PostMapping("analyze")
    @ApiOperation("需求分析")
    public CommonResult analyze(@RequestBody @Valid DemandAnalyzeAddDto dto)
    {
        deDemandInfoService.analyze(dto);
        return CommonResult.success();
    }

    /**
     * 需求分析
     */
    @PreAuthorize("@ss.hasPermi('de:demand:info:rating')")
    @PostMapping("rating")
    @ApiOperation("需求定性")
    public CommonResult rating(@RequestBody @Valid DemandRatingAddDto dto)
    {
        deDemandInfoService.rating(dto);
        return CommonResult.success();
    }

    /**
     * 需求分析
     */
    @PreAuthorize("@ss.hasPermi('de:demand:info:make')")
    @PostMapping("make")
    @ApiOperation("需求处理")
    public CommonResult make(@RequestBody @Valid DemandMakeDto dto)
    {
        deDemandInfoService.make(dto);
        return CommonResult.success();
    }

    /**
     * 获取需求-需求信息详细信息
     */
    @GetMapping(value = "/{id}")
    @ApiOperation("需求详情")
    public CommonResult<DemandDetailVO> getInfo(@PathVariable("id") Long id)
    {
        return CommonResult.success(deDemandInfoService.getInfo(id));
    }

    /**
     * 获取需求-需求信息详细信息
     */
    @GetMapping(value = "task/over/{id}")
    @ApiOperation("需求任务是否完成")
    public CommonResult<Boolean> taskOverInfo(@PathVariable("id") Long id)
    {
        return CommonResult.success(taskInfoService.taskOverInfo(id));
    }

    /**
     * 需求流程查询
     */
    @GetMapping(value = "process/{id}")
    @ApiOperation("需求流程")
    public CommonResult<DemandProcessVO> getProcessInfo(@PathVariable("id") Long id)
    {
        return CommonResult.success(processService.getProcessInfo(id));
    }

    /**
     * 需求流程查询
     */
    @GetMapping(value = "task/{id}")
    @ApiOperation("需求任务列表")
    public CommonResult<DemandTaskVO> getTaskInfo(@PathVariable("id") Long id)
    {
        return CommonResult.success(deDemandInfoService.getTaskInfo(id));
    }

    /**
     * 需求流程查询
     */
    @GetMapping(value = "demand/type")
    @ApiOperation("需求类型列表")
    public CommonResult<List<DemandTypeVO>> getDemandType(@RequestBody DemandInfoPageDto dto)
    {
        return CommonResult.success(deDemandInfoService.getDemandType(dto));
    }




}
